package com.ymttest.database.sharding;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.junit.BeforeClass;
import org.junit.Test;
import org.mybatis.spring.SqlSessionUtils;

import com.ymt.core.tool.Logger;
import com.ymttest.database.sharding.config.DbShardingSourceConfig;
import com.ymttest.database.sharding.db.sharding.mapper.MainOrderMapper;
import com.ymttest.database.sharding.db.sharding.mapper.OrderMapper;
import com.ymttest.database.sharding.db.sharding.model.MainOrderExample;
import com.ymttest.database.sharding.db.sharding.model.MainOrderPo;
import com.ymttest.database.sharding.db.sharding.model.OrderExample;
import com.ymttest.database.sharding.db.sharding.model.OrderPo;

public class ShardingTest {

	private static DbShardingSourceConfig dbShardingSource = DbShardingSourceConfig
			.getInstance();

	public static OrderMapper orderMapper;
	public static MainOrderMapper mainOrderMapper;

	@BeforeClass
	public static void setUp() {

		try {

			SqlSession session = dbShardingSource.shardingSessionFactory()
					.openSession();

			SqlSession session2 = SqlSessionUtils
					.getSqlSession(dbShardingSource.shardingSessionFactory());

			orderMapper = dbShardingSource.shardingSessionFactory()
					.openSession().getMapper(OrderMapper.class);

			mainOrderMapper = dbShardingSource.shardingSessionFactory()
					.openSession().getMapper(MainOrderMapper.class);

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	@Test
	public void testOrderMapper() {
		try {

			OrderPo po1 = new OrderPo();
			po1.setOrderId(981105L);
			po1.setMainOrderId(761105L);
			po1.setUserId(111104L);
			po1.setSellerId(3383L);
			po1.setOrderStatus(1);
			po1.setTotalPrice(new BigDecimal("100.00"));
			po1.setPayableAmount(new BigDecimal("0.00"));
			po1.setAutoCancelHour(new BigDecimal("0.25"));
			po1.setLogisticsType(8);
			po1.setAddressId(11111L);
			po1.setPresale(false);

			orderMapper.insertSelective(po1);

			OrderPo po2 = new OrderPo();

			po2.setOrderId(982203L);
			po2.setMainOrderId(762203L);
			po2.setUserId(112203L);
			po2.setSellerId(3383L);
			po2.setOrderStatus(1);
			po2.setTotalPrice(new BigDecimal("200.00"));
			po2.setPayableAmount(new BigDecimal("0.00"));
			po2.setAutoCancelHour(new BigDecimal("0.25"));
			po2.setLogisticsType(8);
			po2.setAddressId(11111L);
			po2.setPresale(false);

			orderMapper.insertSelective(po2);

			StringBuilder sb = new StringBuilder();

			OrderExample examplePo = new OrderExample();

			examplePo.createCriteria().andOrderIdIn(
					Arrays.asList(981104L, 982203L));

			List<OrderPo> pos = orderMapper.selectByExample(examplePo);
			sb.append(pos);
			sb.append("\r\n");

			examplePo.createCriteria().andMainOrderIdIn(
					Arrays.asList(761104L, 762203L));
			pos = orderMapper.selectByExample(examplePo);

			sb.append(pos);
			sb.append("\r\n");

			System.out.println(sb.toString());

		} catch (Exception e) {
			e.printStackTrace();
		}

	}
	@Test
	public void testMainOrderMapper() {
		MainOrderPo po1 = new MainOrderPo();
		po1.setMainOrderId(1104L);

		mainOrderMapper.insertSelective(po1);

		MainOrderPo po2 = new MainOrderPo();
		po2.setMainOrderId(2203L);
		mainOrderMapper.insertSelective(po2);

		StringBuilder sb = new StringBuilder();

		MainOrderExample example = new MainOrderExample();
		example.createCriteria().andMainOrderIdIn(Arrays.asList(2003L, 2004L));
		List<MainOrderPo> mPos = mainOrderMapper.selectByExample(example);
		sb.append(mPos);
		sb.append("\r\n");

		System.out.println(sb.toString());
	}

	@Test
	public void testOrderPoMapper() {

		long orderId = 1052810095L;

		OrderExample example = new OrderExample();

		example.createCriteria().andOrderIdEqualTo(orderId);

		List<OrderPo> mPos =

		orderMapper.selectByExample(example);

		Logger.info(mPos);
	}
}
